<?php

namespace app\admin\model\manufacture;

use think\Model;

class EquipmentCategory extends Model
{
    // 表名
    protected $name = 'manufacture_equipment_category';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    
    // 追加属性
    protected $append = [
        'status_text'
    ];
    
    // 状态
    public function getStatusList()
    {
        return [
            '1' => __('启用'),
            '0' => __('停用')
        ];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    // 关联设备
    public function equipments()
    {
        return $this->hasMany('Equipment', 'category_id', 'id');
    }

    // 获取子分类
    public function children()
    {
        return $this->hasMany('EquipmentCategory', 'parent_id', 'id');
    }

    // 获取父分类
    public function parent()
    {
        return $this->belongsTo('EquipmentCategory', 'parent_id', 'id');
    }
}
